package com.example.service.impl;

import com.example.domain.BackMessage;
import com.example.domain.Friend;
import com.example.domain.Message;
import com.example.domain.User;
import com.example.mapper.FriendMapper;
import com.example.mapper.MessageMapper;
import com.example.mapper.UserMapper;
import com.example.service.BackMessageType;
import com.example.service.FriendService;
import com.example.service.MessageType;
import com.example.util.IDUtil;
import com.example.websocket.WebSocketServer;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.websocket.Session;
import java.util.*;

/**
 * @author zhouxl
 * @DATE 2021/2/2
 * @descriptoin
 */
@Component
public class FriendServiceImpl implements FriendService {
    @Autowired
    private FriendMapper friendMapper;
    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public BackMessage addFriend(Message msg, Session session) {
        /**
         * 保存当前信息，设置未读
         * 1.如果当前好友在线
         *  1.1.将消息发送给好友
         * 2.不在线
         */
        msg.setMid(IDUtil.UUID());
        msg.setMsgDate(new Date());
        msg.setReadType(MessageType.MSG_READ_TYPE_NO);
        int update = messageMapper.addMessage(msg);
        BackMessage backMessage = new BackMessage();
        BeanUtils.copyProperties(msg, backMessage);
        /**
         * 当前消息判断
         * 1.返回消息类型，成功，失败
         * 2.是否需要弹框，1-弹框，0-非弹框
         * 3.是否需要发送改消息，1-发送，0-不发送
         */
        String uid = msg.getToUser();
        backMessage.setMsgFlag(update == 0 ? "400" : "200");
        backMessage.setSendFlag(WebSocketServer.isLogin(uid) ? "1" : "0");
        backMessage.setDialog("1");
        // TODO 后续需要完善处理请求信息
        backMessage.setContent("添加好友请求");
        User fromUser = userMapper.findByUid(msg.getFromUser());
        User toUser = userMapper.findByUid(msg.getToUser());
        if (Objects.nonNull(fromUser)) {
            backMessage.setFromUserName(fromUser.getName());
        }
        if (Objects.nonNull(toUser)) {
            backMessage.setToUserName(toUser.getName());
        }
        return backMessage;
    }

    @Override
    public BackMessage rejectFriend(Message msg, Session session) {
        msg.setMid(IDUtil.UUID());
        msg.setMsgDate(new Date());
        msg.setReadType(MessageType.MSG_READ_TYPE_NO);
        int update = messageMapper.addMessage(msg);

        BackMessage backMessage = new BackMessage();
        BeanUtils.copyProperties(msg, backMessage);
        String uid = msg.getToUser();
        backMessage.setMsgFlag(update == 0 ? "400" : "200");
        backMessage.setSendFlag(WebSocketServer.isLogin(uid) ? "1" : "0");
        backMessage.setDialog("1");
        backMessage.setContent("拒绝添加好友");
        User fromUser = userMapper.findByUid(msg.getFromUser());
        User toUser = userMapper.findByUid(msg.getToUser());
        if (Objects.nonNull(fromUser)) {
            backMessage.setFromUserName(fromUser.getName());
        }
        if (Objects.nonNull(toUser)) {
            backMessage.setToUserName(toUser.getName());
        }
        return backMessage;
    }

    @Override
    public BackMessage agreeFriend(Message msg, Session session) {
        msg.setMid(IDUtil.UUID());
        msg.setMsgDate(new Date());
        msg.setReadType(MessageType.MSG_READ_TYPE_NO);
        int update = messageMapper.addMessage(msg);

        // 添加好友信息
        Friend friend = new Friend();
        friend.setFid(IDUtil.UUID());
        friend.setUid(msg.getToUser());
        friend.setFriendId(msg.getFromUser());
        int friendUpdate = friendMapper.addFriend(friend);

        friend = new Friend();
        friend.setFid(IDUtil.UUID());
        friend.setUid(msg.getFromUser());
        friend.setFriendId(msg.getToUser());
        friendUpdate = friendMapper.addFriend(friend);

        BackMessage backMessage = new BackMessage();
        BeanUtils.copyProperties(msg, backMessage);
        String uid = msg.getToUser();
        backMessage.setMsgFlag(update == 0 ? "400" : "200");
        backMessage.setSendFlag(WebSocketServer.isLogin(uid) ? "1" : "0");
        backMessage.setDialog("1");
        backMessage.setContent("同意添加好友,你可以和他聊天了...");
        User fromUser = userMapper.findByUid(msg.getFromUser());
        User toUser = userMapper.findByUid(msg.getToUser());
        if (Objects.nonNull(fromUser)) {
            backMessage.setFromUserName(fromUser.getName());
        }
        if (Objects.nonNull(toUser)) {
            backMessage.setToUserName(toUser.getName());
        }
        return backMessage;
    }

    @Override
    public List<BackMessage> broadcaseLoginMessage(String uid, Session session) {
        List<BackMessage> messages = new ArrayList<>();
        List<Map<String, Object>> friendList = userMapper.queryFriendsInfo(uid);
        for (Map<String, Object> m : friendList) {
            String toUser = m.get("uid").toString();
            if (!WebSocketServer.isLogin(toUser)) {
                continue;
            }
            BackMessage backMessage = new BackMessage();
            backMessage.setFromUser(uid);
            backMessage.setToUser(toUser);
            backMessage.setMsgFlag(BackMessageType.MSG_FLAG_SUCCESS);
            backMessage.setMsgType(BackMessageType.MSG_TYPE_FRIEND_LOGIN);
            User fromUseInfo = userMapper.findByUid(uid);
            User toUserInfo = userMapper.findByUid(toUser);
            backMessage.setFromUserName(fromUseInfo.getName());
            backMessage.setToUserName(toUserInfo.getName());
            String content = "好友上线了";
            backMessage.setContent(content);
            backMessage.setSendFlag(BackMessageType.SEND_FLAG_YES);
            backMessage.setDialog(BackMessageType.DIALOG_YES);
            messages.add(backMessage);
        }
        return messages;
    }
}
